package com.atguigu.springboot.controller;

import com.atguigu.springboot.exception.UserNotExistException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

//在springMVC中要想将一个controller成为一个异常处理器,就得加@ControllerAdvice
@ControllerAdvice
public class MyExceptionHandler {
	

	//处理UserNotExistException类型的异常
	@ExceptionHandler(UserNotExistException.class)
	public String userNotExistException(Model model, HttpServletRequest request){
		//修改request中的该请求的状态码
		request.setAttribute("javax.servlet.error.status_code",500);
		//添加我们自己定制的错误信息
		model.addAttribute("data","出错了");
		model.addAttribute("message_1","出错了!");
		//通过forward来转发到error请求,来让springboot处理这个请求完成对html和json数据的转换
		return "forward:/error";
	}
}
